﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Text;
using System.Drawing;

namespace HYSoft.Web
{
    public partial class sensor55_list : System.Web.UI.Page
    {
        public int type = 0;
        public int id = 0;
        public int areano = 0;
        public int mac = 0;
        public string ifylj = string.Empty;
        public string bplx = string.Empty;
        public string tux;
        public string cheryalue3;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Getandsetquerydata())
            {
                Response.Write("<script lanuage='javascript'>alert('您请求的页面或传递的参数不存在，点击【确定】返回上一页');history.go(-1);</script>");
                return;
            }
            GetJZInfo();
            if (bplx == "0")
            {
                Getcddata();
            }
            InitChart2();
            Getcdyldata();
            data_tux();
        }

        protected bool Getandsetquerydata()
        {
            bool temp = false;
            DataTable dt = new DataTable();
            mac = Convert.ToInt32(Request.QueryString["mac"]);
            id = Convert.ToInt32(Request.QueryString["id"]);
            dt = new BLL.gprsNode().GetList("mac=" + mac.ToString()).Tables[0];
            if (dt.Rows.Count == 1)
            {
                if (dt.Rows[0]["value1"].ToString() == "1")
                {
                    Response.Redirect("sensor55_listTest.aspx?id=" + id.ToString());
                }
                type = Convert.ToInt32(dt.Rows[0]["adminno"]);
                areano = Convert.ToInt32(dt.Rows[0]["areano"]);
                id = Convert.ToInt32(dt.Rows[0]["index"]);
                temp = true;
            }
            else
            {
                temp = false;
            }
            return temp;
        }

        protected void GetJZInfo()
        {
            DataTable dt = new DataTable();
            dt = new BLL.usr2node().GetList("[index]=" + id.ToString()).Tables[0];
            if (dt.Rows.Count > 0)
            {
                ifylj = dt.Rows[0]["value0"].ToString();
                bplx = dt.Rows[0]["value1"].ToString();
                this.Repeater1.DataSource = dt;
                this.Repeater1.DataBind();
            }
        }

        protected string data_addrname(object id)
        {
            string addrname = "";
            DataTable dt = new BLL.monitor().GetList("[index] = " + int.Parse(id.ToString())).Tables[0];
            if (dt.Rows.Count > 0)
            {
                addrname = dt.Rows[0]["addr"].ToString();
            }
            return addrname;
        }

        protected string IfYlj(object obj)
        {
            if (obj.ToString() == "0")
                return "无";
            else
                return "有";
        }

        protected string BpLx(object obj)
        {
            if (obj.ToString() == "0")
                return "地表变形";
            else
                return "深部位移";
        }

        protected void data_tux()
        {
            DataTable dt = new BLL.gprsNode().GetList("[mac] = " + mac.ToString()).Tables[0];
            tux = dt.Rows[0]["tux"].ToString();
            cheryalue3 = dt.Rows[0]["charvalue3"].ToString();
            dt.Clear();
            dt.Dispose();
        }

        protected void Getcdyldata()
        {
            if (ifylj == "1")
            {
                DataTable dt = new BLL.bpcddata().Listmany("select * from (select top 20 cdid, cldate,ljbpjyl from tb_bpcddata where cdbh=1 order by cdid desc) as A order by cdid asc").Tables[0];

                string[] xvalue = new string[dt.Rows.Count];
                Int64[] yvalue = new Int64[dt.Rows.Count];//new long[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    xvalue[i] = dt.Rows[i]["cldate"].ToString();
                    yvalue[i] = Convert.ToInt64(dt.Rows[i]["ljbpjyl"].ToString()) * 10;
                }
                Chart2.Series[0].Points.DataBindXY(xvalue, yvalue);
                Chart2.ChartAreas["ChartArea1"].AxisX.Interval = 5;
                Chart2.ChartAreas["ChartArea1"].AxisY.Interval = 5000;
            }
            else
            {
                Chart2.Visible = false;
            }
        }

        private void InitChart2()
        {
            Chart2.Series["Series1"].Points.Clear();
            Chart2.Series["Series1"].IsValueShownAsLabel = true;
            Chart2.Series["Series1"].MarkerStyle = MarkerStyle.Circle;
            Chart2.Series["Series1"].MarkerSize = 10;
            Chart2.Series["Series1"].LabelForeColor = Color.Purple;
            Chart2.Series["Series1"].MarkerBorderWidth = 2;
            //Chart2.Series["Series1"].Color = Color.SkyBlue;
            Chart2.Series["Series1"].BorderWidth = 2;
            Chart2.Series["Series1"].ShadowOffset = 1;
            Chart2.Series["Series1"].LabelFormat = "0mm";


            Chart2.ChartAreas["ChartArea1"].AxisX.Title = "记录时间";
            Chart2.ChartAreas["ChartArea1"].AxisY.Title = "毫米(mm)";

            Chart2.ChartAreas["ChartArea1"].AxisX.TitleFont = new Font("宋体", 11);
            Chart2.ChartAreas["ChartArea1"].AxisY.TitleFont = new Font("宋体", 11);
        }

        protected void Getcddata()
        {
            //DataTable dt = new BLL.bpcddata().Listmany("select cdbh from tb_bpcddata group by cdbh  order by cdbh asc").Tables[0];
            //StringBuilder sqlstr = new StringBuilder();
            //int tag = 0;
            //foreach (DataRow row in dt.Rows)
            //{
            //    tag++;
            //    sqlstr.Append("select * from (select top 1 * from tb_bpcddata where cdbh=" + row["cdbh"].ToString() + "  order by cdid desc) as T" + row["cdbh"].ToString());
            //    if (tag < dt.Rows.Count)
            //    {
            //        sqlstr.Append(" union ");
            //    }
            //}
            //sqlstr.Append(" order by cdbh ");
            //dt = new BLL.bpcddata().Listmany(sqlstr.ToString()).Tables[0];
            DataTable dt = new BLL.usr2node().GetList("[index]=" + id.ToString()).Tables[0];
            if (dt.Rows.Count > 0)
            {
                dt = new BLL.bpcddata().Listmany("select * from (select top 10 * from tb_bpcddata where ljbp_sec_id=" + dt.Rows[0]["areano"] + " order by cdid desc) as b order by cdid").Tables[0];
                Repeater4.DataSource = dt;
                Repeater4.DataBind();
            }
        }

        protected string Clc(object num)
        {
            return (Convert.ToDouble(num) / 1000).ToString("0.000");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string from = this.txtfrom.Value.TrimEnd();
            string to = this.txtto.Value.TrimEnd();
            DataTable dt = new BLL.usr2node().GetList("[index]=" + id.ToString()).Tables[0];
            if (dt.Rows.Count > 0)
            {
                dt = new BLL.bpcddata().Listmany("select * from tb_bpcddata where ljbp_sec_id=" + dt.Rows[0]["areano"] + " and cldate > '" + from + "' and cldate < '" + to + "' order by cdid desc").Tables[0];
                Repeater4.DataSource = dt;
                Repeater4.DataBind();
            }
        }
    }
}